<?php
/**
 * 数据资源抽象类
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Source.php 1059 2011-11-18 09:26:15Z mmxie $
 */
abstract class IG_Data_Source extends IG_Data_FactoryAbstract
{
    protected $_request;

    protected $_rows = array();

    protected $_columns = array();

    protected $_params = array();

    protected $_paginator;

    /**
     * @return Zend_Paginator
     */
    public function getPaginator()
    {
        return $this->_paginator;
    }

    public function hasPaginator()
    {
        return $this->_paginator instanceof Zend_Paginator;
    }

    public function setPaginator(Zend_Paginator $paginator)
    {
        $this->_paginator = $paginator;
        return $this;
    }

    /**
     * IG_Data_Source
     * @param array|Zend_Config $config
     * @return IG_Data_Source
     */
    public static function factory($config)
    {
        return self::_factory($config, __CLASS__);
    }

    /**
     *
     * @param Zend_Config|array $config
     */
    public function __construct($config)
    {
        foreach (IG_Config::convArray($config) as $key => $val) {
            $this->{'set' . ucfirst($key)}($val);
        }
        IG_Data::$adapter->getView()->assign('source', $this);
    }

    /**
     * @return Zend_Controller_Request_Abstract
     */
    public function getRequest()
    {
        return IG_Data::$adapter->getRequest();
    }

    public function __call($method, $params)
    {
        throw new IG_Exception("未知方法 '$method'");
    }


    public function &getRows()
    {
        return $this->_rows;
    }

    /**
     * set data
     * @param array $data
     * @return IG_Data_Source
     */
    public function setRows(array $rows)
    {
        $this->_rows = $rows;
        return $this;
    }

    public function getColumns()
    {
        return $this->_columns;
    }

    /**
     * @return IG_Data_Source
     */
    public function setColumns($columns)
    {
        $this->_columns = $columns;
        return $this;
    }

    public function setParams(array $params)
    {
        $this->_params = $params;
        return $this;
    }

    public function getParams()
    {
        return $this->_params;
    }

    /**
     * @return IG_Data_Source
     */
    abstract public function run();
}